﻿using System;
using System . Collections . Generic;
using System . IO;
using System . Linq;
using System . Runtime . InteropServices . WindowsRuntime;
using Windows . Foundation;
using Windows . Foundation . Collections;
using Windows . UI;
using Windows . UI . Xaml;
using Windows . UI . Xaml . Controls;
using Windows . UI . ViewManagement;
using Windows . UI . Xaml . Media . Animation;
using Windows . UI . Xaml . Controls . Primitives;
using Windows . UI . Xaml . Data;
using Windows . UI . Xaml . Input;
using Windows . UI . Xaml . Media;
using Windows . UI . Xaml . Navigation;
using Windows . Storage;
using Windows . Media . Capture;
using System . Threading . Tasks;
using Windows . Media . MediaProperties;

// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkID=390556

namespace WenceyWang . RichMan4L . App . Pages
{
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class StartPage : Page
    {
        public StartPage ( )
        {
            this . InitializeComponent ( );
        }

        /// <summary>
        /// Invoked when this page is about to be displayed in a Frame.
        /// </summary>
        /// <param name="e">Event data that describes how this page was reached.
        /// This parameter is typically used to configure the page.</param>
        protected override void OnNavigatedTo ( NavigationEventArgs e )
        {

        }

        private void Page_Loaded ( object sender , RoutedEventArgs e )
        {
#if WINDOWS_PHONE_APP

            StatusBar . GetForCurrentView ( ) . HideAsync ( ) . GetResults ( );

#endif 
            StartStoryBoard . Completed += StartStoryBoard_Completed;
#if RecordVideo
            DispatcherTimer t=new DispatcherTimer();
            DispatcherTimer s=new DispatcherTimer();
            s . Interval = TimeSpan . FromSeconds ( 61 );
            t . Interval = TimeSpan . FromSeconds ( 1 );
            t . Tick += async ( a , b ) =>
            {
                if ( !App . IsRecording )
                {
                    if ( App . CurrentCapture == null )
                    {
                        App . InitailizeCapureAsync ( );
                    }
                    StorageFolder vdlib = KnownFolders.VideosLibrary;
                    StorageFile newfile =await  vdlib.CreateFileAsync(DateTime.Now.ToString("yyyyMMddHHmmss") + ".mp4", CreationCollisionOption.ReplaceExisting);
                    // 开始录制视频
                    await App . CurrentCapture . StartRecordToStorageFileAsync ( MediaEncodingProfile . CreateMp4 ( VideoEncodingQuality . Auto ) , newfile );
                    // 标识已经开始录制
                    App . IsRecording = true;
                    t . Stop ( );
                }
            };
            s . Tick += ( a , b ) =>
             {
                 App . CleanupCaptureAsync ( );
                 s . Stop ( );
             };
#endif
            StartStoryBoard . Begin ( );
#if RecordVideo
            t . Start ( );
            s . Start ( );
#endif
        }

        private void StartStoryBoard_Completed ( object sender , object e )
        {

            EasyPage . Navigate ( typeof ( MainPage ) , null , "Cyan" , LeaveStoryBoard , BackGroundRect , Frame );
        }
    }
}
